
// LoginModel 用于处理登录请求
class LoginModel {
  final int code;
  final String? msg;
  final AiTokenModelOrBool? data;

  LoginModel({
    required this.code,
    this.msg,
    this.data,
  });

  // 自定义解码器
  factory LoginModel.fromJson(Map<String, dynamic> json) {
    var dataJson = json['data'];
    AiTokenModelOrBool? data;

    if (dataJson is Map<String, dynamic>) {
      // 如果 data 是一个对象，解码成 AiTokenModel
      data = AiTokenModelOrBool.model(AiTokenModel.fromJson(dataJson));
    } else if (dataJson is bool) {
      // 如果 data 是一个布尔值
      data = AiTokenModelOrBool.bool(dataJson);
    }

    return LoginModel(
      code: json['code'],
      msg: json['msg'],
      data: data,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

// 定义一个枚举，用于存储 AiTokenModel 或 Bool 类型的 data
class AiTokenModelOrBool {
  final AiTokenModel? model;
  final bool? boolValue;

  AiTokenModelOrBool.model(this.model) : boolValue = null;
  AiTokenModelOrBool.bool(this.boolValue) : model = null;

  // toJson 方法用于将类型转换为 JSON
  Map<String, dynamic>? toJson() {
    if (model != null) {
      return model?.toJson();
    } else if (boolValue != null) {
      return {'bool': boolValue};
    }
    return null;
  }
}

// AiTokenModel 用于存储 token 和过期时间等信息
class AiTokenModel {
  final String accessToken;
  final int? expiresIn;
  final int? status;

  AiTokenModel({
    required this.accessToken,
    this.expiresIn,
    this.status,
  });

  factory AiTokenModel.fromJson(Map<String, dynamic> json) {
    return AiTokenModel(
      accessToken: json['access_token'],
      expiresIn: json['expires_in'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'access_token': accessToken,
      'expires_in': expiresIn,
      'status': status,
    };
  }
}

// UserData 用于存储用户数据
class UserData {
  final int code;
  final String msg;
  final MemberInfo? data;

  UserData({
    required this.code,
    required this.msg,
    this.data,
  });

  factory UserData.fromJson(Map<String, dynamic> json) {
    return UserData(
      code: json['code'],
      msg: json['msg'],
      data: json['data'] != null ? MemberInfo.fromJson(json['data']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

// UsssInfo 用于存储用户信息
class MemberInfo {
  final String countryEn;
  final int memberId;
  final EmailInfo? email;  // 可以为 null
  final MobileInfo? mobile;  // 可以为 null
  final LevelInfo? level;  // 可以为 null
  final String nickName;
  final String avatar;
  final String memberName;
  final String regLanguage;
  final String defaultLanguage;

  MemberInfo({
    required this.countryEn,
    required this.memberId,
    this.email,  // 允许为空
    this.mobile,  // 允许为空
    this.level,  // 允许为空
    required this.nickName,
    required this.avatar,
    required this.memberName,
    required this.regLanguage,
    required this.defaultLanguage,
  });

  factory MemberInfo.fromJson(Map<String, dynamic> json) {
    return MemberInfo(
      countryEn: json['countryEn'],
      memberId: json['memberId'],
      email: json['email'] != null ? EmailInfo.fromJson(json['email']) : null,
      mobile: json['mobile'] != null ? MobileInfo.fromJson(json['mobile']) : null,
      level: json['level'] != null ? LevelInfo.fromJson(json['level']) : null,
      nickName: json['nickName'],
      avatar: json['avatar'],
      memberName: json['memberName'],
      regLanguage: json['regLanguage'],
      defaultLanguage: json['defaultLanguage'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'countryEn': countryEn,
      'memberId': memberId,
      'email': email?.toJson(),  // 只有 email 不为 null 时才调用 toJson
      'mobile': mobile?.toJson(),  // 只有 mobile 不为 null 时才调用 toJson
      'level': level?.toJson(),  // 只有 level 不为 null 时才调用 toJson
      'nickName': nickName,
      'avatar': avatar,
      'memberName': memberName,
      'regLanguage': regLanguage,
      'defaultLanguage': defaultLanguage,
    };
  }
}

class EmailInfo {
  final String email;
  final int status;

  EmailInfo({
    required this.email,
    required this.status,
  });

  factory EmailInfo.fromJson(Map<String, dynamic> json) {
    return EmailInfo(
      email: json['email'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'email': email,
      'status': status,
    };
  }
}

class MobileInfo {
  final String code;
  final String mobile;
  final int status;

  MobileInfo({
    required this.code,
    required this.mobile,
    required this.status,
  });

  factory MobileInfo.fromJson(Map<String, dynamic> json) {
    return MobileInfo(
      code: json['code'],
      mobile: json['mobile'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'mobile': mobile,
      'status': status,
    };
  }
}

class LevelInfo {
  final String levelName;
  final String appLevelName;
  final String webDiscount;
  final String appDiscount;

  LevelInfo({
    required this.levelName,
    required this.appLevelName,
    required this.webDiscount,
    required this.appDiscount,
  });

  factory LevelInfo.fromJson(Map<String, dynamic> json) {
    return LevelInfo(
      levelName: json['levelName'],
      appLevelName: json['appLevelName'],
      webDiscount: json['webDiscount'],
      appDiscount: json['appDiscount'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'levelName': levelName,
      'appLevelName': appLevelName,
      'webDiscount': webDiscount,
      'appDiscount': appDiscount,
    };
  }
}

// emailData 用于存储用户的邮箱数据
class emailData {
  final int status;
  final String? value;

  emailData({
    required this.status,
    this.value,
  });

  factory emailData.fromJson(Map<String, dynamic> json) {
    return emailData(
      status: json['status'],
      value: json['value'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'value': value,
    };
  }
}

// levelData 用于存储用户等级信息
class levelData {
  final String appDiscount;
  final String levelName;
  final String webDiscount;
  final String? appLevelName;

  levelData({
    required this.appDiscount,
    required this.levelName,
    required this.webDiscount,
    this.appLevelName,
  });

  factory levelData.fromJson(Map<String, dynamic> json) {
    return levelData(
      appDiscount: json['appDiscount'],
      levelName: json['levelName'],
      webDiscount: json['webDiscount'],
      appLevelName: json['appLevelName'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'appDiscount': appDiscount,
      'levelName': levelName,
      'webDiscount': webDiscount,
      'appLevelName': appLevelName,
    };
  }
}

// phoneData 用于存储用户的手机数据
class phoneData {
  final int status;
  final String? code;
  final String? value;

  phoneData({
    required this.status,
    this.code,
    this.value,
  });

  factory phoneData.fromJson(Map<String, dynamic> json) {
    return phoneData(
      status: json['status'],
      code: json['code'],
      value: json['value'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'code': code,
      'value': value,
    };
  }
}

// 验证码信息
class PhoneCodeModel {
  final int code;
  final String? msg;
  final AiTokenModelOrBool? data;

  PhoneCodeModel({
    required this.code,
    this.msg,
    this.data,
  });

  // 自定义解码器
  factory PhoneCodeModel.fromJson(Map<String, dynamic> json) {
    var dataJson = json['data'];
    AiTokenModelOrBool? data;

    if (dataJson is Map<String, dynamic>) {
      // 如果 data 是一个对象，解码成 AiTokenModel
      data = AiTokenModelOrBool.model(AiTokenModel.fromJson(dataJson));
    } else if (dataJson is bool) {
      // 如果 data 是一个布尔值
      data = AiTokenModelOrBool.bool(dataJson);
    }

    return PhoneCodeModel(
      code: json['code'],
      msg: json['msg'],
      data: data,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

//三方登录model
class ThreeLoginModel {
  final int code;
  final String msg;
  final List<String> data;

  ThreeLoginModel({
    required this.code,
    required this.msg,
    required this.data,
  });

  // 从 JSON 转换为 ThreeLoginModel 对象
  factory ThreeLoginModel.fromJson(Map<String, dynamic> json) {
    return ThreeLoginModel(
      code: json['code'] as int,
      msg: json['msg'] as String,
      data: List<String>.from(json['data'] as List),
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data,
    };
  }
}

class UserBingData {
  int code;
  String msg;
  UsssBingInfo? data;

  UserBingData({
    required this.code,
    required this.msg,
    this.data,
  });

  factory UserBingData.fromJson(Map<String, dynamic> json) {
    return UserBingData(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] != null ? UsssBingInfo.fromJson(json['data']) : null,
    );
  }
}

class UsssBingInfo {
  String bizId;
  String bindType;
  String? email;
  String? mobile;

  UsssBingInfo({
    this.bizId = '',
    this.bindType = '',
    this.email,
    this.mobile,
  });

  factory UsssBingInfo.fromJson(Map<String, dynamic> json) {
    return UsssBingInfo(
      bizId: json['bizId'] ?? '',
      bindType: json['bindType'] ?? '',
      email: json['email'],
      mobile: json['mobile'],
    );
  }
}
